package Aleehot100;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: c057
 * Package: Aleehot100
 * Description: 17. 电话号码的字母组合
 *
 * @Author BCXJ
 * @Create 2025/8/8 20:49
 * @Version 1.0
 * @Since 1.0
 */
public class c057 {

    static Map<Character, String> map = new HashMap<>();
    static {
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
    }

    public static List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<String>();
        if(digits.isEmpty()) {
            return res;
        }

        process(digits, res, 0, new StringBuilder()); // 其实也是回溯
        return res;
    }


    private static void process(String digits, List<String> res, int index, StringBuilder stringBuilder) {
        if(index == digits.length()) {
            res.add(stringBuilder.toString());
            return;
        }
        String str = map.get(digits.charAt(index)); // 根据下标获取对应的 字母
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            stringBuilder.append(charArray[i]);
            process(digits, res, index + 1, stringBuilder);
            stringBuilder.deleteCharAt(index);
        }
    }


    // 做这道题之前，需要熟悉 StringBuilder 的api
    public static void main(String[] args) {
        StringBuilder strBuider = new StringBuilder();
        strBuider.append("123");
        strBuider.deleteCharAt(1); // 0下标为起始
        System.out.println(strBuider);
    }
}
